package com.kuaimacode.kframework.agent.services.sysparam;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.sysparam.SysParamMapper;
import com.kuaimacode.kframework.mybatis.models.sysparam.SysParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author anzy
 * @date 2019/05/17
 */
@Service
public class SysParamService extends ServiceImpl<SysParamMapper, SysParam> {

    	/**
    	 *  分页查找所有的数据
    	 * @param pageNum 当前页码，从1开始
    	 * @param pageSize  一页显示多少行
    	 * @return
    	 */
    	public PageInfo<SysParam> findAll(int pageNum, int pageSize) {
    		PageHelper.startPage(pageNum, pageSize);
    		QueryWrapper queryWrapper = new QueryWrapper();
    		queryWrapper.eq("agent_id", SessionUtil.getAgentId());
    		List<SysParam> datas = baseMapper.selectList(queryWrapper);
    		return new PageInfo<SysParam>(datas);
    	}

	/**
	 *  查找临时加价相关配置
	 * @return
	 */
	public List<SysParam> findAllFeeConfig() {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("agent_id", SessionUtil.getAgentId());
		queryWrapper.likeRight("name", "FEE");
		List<SysParam> datas = baseMapper.selectList(queryWrapper);
		return datas;
	}


	public SysParam findByName(String name,String agentId){
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("name",name);
		queryWrapper.eq("agent_id",agentId);
		return baseMapper.selectOne(queryWrapper);
	}

    /**
     * 当开启一个时，设置其他都关闭
     * @param  name 需要开启的参数
     */
    @Transactional
    public void offAllAndOpenSelf(String name){
        //关闭其他
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("agent_id",SessionUtil.getAgentId());
        updateWrapper.likeRight("name", "FEE");
        updateWrapper.ne("name",name);

        SysParam offSysParam = new SysParam();
        offSysParam.setValue(Constant.SYS_PARAM_SWITCH_OFF);
        baseMapper.update(offSysParam,updateWrapper);

        //开启自己
        UpdateWrapper updateWrapper1 = new UpdateWrapper();
        updateWrapper1.eq("agent_id",SessionUtil.getAgentId());
        updateWrapper1.likeRight("name", "FEE");
        updateWrapper1.eq("name",name);

        SysParam onSysParam = new SysParam();
        onSysParam.setValue(Constant.SYS_PARAM_SWITCH_ON);
        baseMapper.update(onSysParam,updateWrapper1);


    }


}

